let login=document.getElementById('login')
let register=document.getElementById('register')
let loginPassword_box=document.getElementsByClassName('loginPassword-box')[0]
let loginCode_box=document.getElementsByClassName('loginCode-box')[0]
let form_box=document.getElementsByClassName('form-box')[0]
const input_code=document.getElementsByClassName('input_code')[0]
register.addEventListener('click',()=>{
    form_box.style.transform='translateX(80%)'
    loginPassword_box.style.display='none'
    loginCode_box.style.display='block'
})
login.addEventListener('click',()=>{
    form_box.style.transform='translateX(0%)'
    loginCode_box.style.display='none'
    loginPassword_box.style.display='flex'
})
function sendCode(email,btn) {

    const filteredEmail=email.replace(/<[^>]*>/g, '')
    console.log(btn)


    if(filteredEmail){
        $.ajax({
            url:  '/login/loginCode/sendCode',
            type: 'POST',
            data: {email: email},
            success: function(result){
                showCustomAlert('获取验证码成功',3000)
                refresh(btn,60);
            },
            error:function(error){
                showCustomAlert('请求出错: ' + error.status + ' ' + error.statusText, 3000);
                console.log(error);
            }

        })
    }else{
        showCustomAlert('邮箱为空，请输入邮箱',3000)
    }

}




//
// const  input_emails=document.getElementsByClassName(".input_txt")
// for(let i=0;i<input_emails.length;i++){
//     input_emails[i].addEventListener('input',validateEmail)
//
// }


//
// const input_code=document.getElementsByClassName('input_code')[0]
// let input_email2=document.getElementsByClassName('input_txt2')[0]
function dosubmit2(){

    if(input_email2.value.trim()===''){
        event.preventDefault();
        input_email2.value='请输入邮箱地址'
        input_email2.style.color='red'
    }
    if(input_code.value.trim()===''){
        event.preventDefault();
        input_code.value='不能为空'
        input_code.style.color='red'

    }
    if(input_email2.value.trim()==='请输入邮箱地址'){
        event.preventDefault();

    }
    if(input_code.value.trim()==='不能为空'){
        event.preventDefault();


    }
}


input_code.addEventListener('focus',function(){
    if(this.value==='不能为空'){
        this.value=''
        this.style.color='white'
    }
})

const openEyes=document.querySelector('.openEyes')
const closeEyes=document.querySelector('.closeEyes')
const inputPassword1=document.getElementsByClassName('input_password')[0]
openEyes.addEventListener('click',function(){
    openEyes.style.display='none'
    closeEyes.style.display='flex'
    inputPassword1.type='password'
})
closeEyes.addEventListener('click',function(){
    openEyes.style.display='flex'
    closeEyes.style.display='none'
    inputPassword1.type='text'
})




function showCustomAlert(message, duration) {
    let customAlert = document.getElementById('customAlert');
    if (!customAlert) {
         div_outter=document.createElement('div');
        customAlert = document.createElement('div');
        customAlert.id = 'customAlert';
         // customAlert.style.margin='7px 12px';
        const alertMessage = document.createElement('p');
        alertMessage.id = 'alertMessage';
        customAlert.appendChild(alertMessage);

        document.body.appendChild(customAlert);
    }

    const alertMessage = document.getElementById('alertMessage');

    alertMessage.textContent = message;
    if(alertMessage.textContent==null){
        customAlert.style.display = 'none';
    }

    customAlert.style.display = 'block';
    setTimeout(() => {
        customAlert.style.display = 'none';
    }, duration);
}
if (errorMessage && errorMessage !== '') {
    showCustomAlert(errorMessage, 3000);
}
function dosubmit(){
    let input_email1=document.getElementsByClassName('input_txt1')[0]
    if(input_email1.value.trim()===''){
        event.preventDefault();
        input_email1.value='请输入邮箱地址'
        input_email1.style.color='red'
    }
    if(inputPassword.value.trim()===''){
        event.preventDefault();
        openEyes.style.display='flex'
        closeEyes.style.display='none'
        inputPassword.type='text'
        inputPassword.value='请输入密码'
        inputPassword.style.color='red'
    }
    if(input_email1.value.trim()==='请输入邮箱地址'){
        event.preventDefault();

    }
    if(inputPassword.value.trim()==='请输入密码'){
        event.preventDefault();

    }
}
// const loginButton=document.getElementsByClassName('button')[0]
// loginButton.addEventListener('click',function(){
//
//     if(input_email.value.trim()===''){
//         input_email.value='请输入邮箱地址'
//         input_email.style.color='red'
//     }
//     if(inputPassword.value.trim()===''){
//         openEyes.style.display='flex'
//         closeEyes.style.display='none'
//         inputPassword.type='text'
//         inputPassword.value='请输入密码'
//         inputPassword.style.color='red'
//     }
// })


// let login=document.getElementsByClassName('button')[0]
// login.addEventListener('click',()=>{
//     const form = document.querySelector('.login-box');
//
//
//     form.addEventListener('submit', function(event) {
//
//         event.preventDefault();
//     })
//
//     })


function validatePassword() {
    var password=document.querySelector(".input_password").value;
    const regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/;
    console.log(regex)
    if (regex.test(password)) {
        console.log(regex)
        document.getElementById('resultPassword1').textContent = '';
    } else {
        document.getElementById('resultPassword1').textContent = '长度需在6到20位之间,且必须含有字母';
    }
}
let inputPassword=document.querySelector('.input_password')
inputPassword.addEventListener('input',validatePassword)


function validateEmail() {
    const  email=document.querySelector(".input_txt1").value;
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    console.log(emailRegex)
    if (emailRegex.test(email)) {
        document.getElementById('resultEmail1').textContent = '';
    } else {
        document.getElementById('resultEmail1').textContent = '邮箱地址无效';
    }
}
let input_email1=document.querySelector('.input_txt1')
input_email1.addEventListener('input',validateEmail)
function validateEmail2() {
    const  email=document.querySelector(".input_txt2").value;
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

    if (emailRegex.test(email)) {
        document.getElementById('resultEmail2').textContent = '';
    } else {
        document.getElementById('resultEmail2').textContent = '邮箱地址无效';
    }
}
let  input_email2=document.querySelector(".input_txt2")
input_email2.addEventListener('input',validateEmail2)
input_email1.addEventListener('focus',function(){
    if(this.value==='请输入邮箱地址'){
        this.value=''
        this.style.color='white'
    }
})
inputPassword.addEventListener('focus',function(){
    console.log(11)
    if(this.value==='请输入密码'){
        this.value=''
        this.style.color='white'

    }
})
input_email2.addEventListener('focus',function(){
    if(this.value==='请输入邮箱地址'){
        this.value=''
        this.style.color='white'
    }
})

function refresh(btn, initialCountdown) {
    let countdown = initialCountdown;
    let timer;

    btn.disabled = true;
    btn.style.pointerEvents = 'none';
    btn.textContent = `${countdown}s后重发`;

    // 启动定时器前，先存储初始倒计时
    localStorage.setItem("countdown", countdown); // 存储总倒计时时间
    localStorage.setItem("startTime", Date.now()); // 存储开始时间（用于计算剩余时间）

    timer = setInterval(() => {
        // 计算剩余时间（避免因页面切换导致的计时偏差）
        const elapsed = Date.now() - parseInt(localStorage.getItem("startTime"));
        const remaining = countdown - Math.floor(elapsed / 1000);

        if (remaining > 0) {
            localStorage.setItem("countdown", remaining); // 更新剩余时间
            btn.textContent = `${remaining}s后重发`;
        } else {
            localStorage.removeItem("countdown");
            localStorage.removeItem("startTime");
            clearInterval(timer);
            btn.disabled = false;
            btn.textContent = "获取验证码";
            btn.style.pointerEvents = 'auto';
        }
    }, 1000);
}

document.addEventListener('DOMContentLoaded', function () {
    const getCodeBtn = document.querySelector('.get-code');

    // 从localStorage获取存储的倒计时状态
    const savedCountdown = localStorage.getItem("countdown");
    const savedStartTime = localStorage.getItem("startTime");

    if (savedCountdown && savedStartTime) {
        // 计算剩余时间
        const elapsed = Date.now() - parseInt(savedStartTime);
        const remaining = parseInt(savedCountdown) - Math.floor(elapsed / 1000);

        if (remaining > 0) {
            // 恢复倒计时
            refresh(getCodeBtn, remaining);
        } else {
            // 清除过期的存储数据
            localStorage.removeItem("countdown");
            localStorage.removeItem("startTime");
        }
    }
});
// 存储时
localStorage.setItem("countdown", String(countdown)); // 转为字符串存储
localStorage.setItem("startTime", String(Date.now())); // 转为字符串存储

// 读取时
const savedCountdown = parseInt(localStorage.getItem("countdown")); // 转为数字
const savedStartTime = parseInt(localStorage.getItem("startTime")); // 转为数字